// 封装核心路由表 （要求有懒加载功能

import React, { Suspense } from 'react'
import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom'
import routerPage from './pages'
import { routerItem } from '../interface'

const Router = () => {

    const mapRouter = (routes: routerItem[]) => {
        return routes.map((item, idx) => {
            return item.path ? <Route key={idx} path={item.path} element={item.element}>
                {
                    item.children && mapRouter(item.children)
                }
            </Route> : <Route key={idx} path={item.from} element={<Navigate to={item.to} />}></Route>
        })
    }

    return <Suspense fallback={<div>加载中。。。</div>}>
        <BrowserRouter>
            <Routes>
                {
                    mapRouter(routerPage)
                }
            </Routes>
        </BrowserRouter>
    </Suspense>
}

export default Router